<template>
    <div class="resume-education">
      <div class="education" v-if="!editEducation">
        <h2>教育经历<span data-html2canvas-ignore="true" @click="editOpen('editEducation')"><i class="el-icon-circle-plus-outline"></i>添加</span></h2>
        <div class="content" v-if="educationList.length === 0">
            <el-empty :image-size="200"></el-empty>
        </div>
        <div class="content" v-else>
          <div class="education-wrapper" v-for="(education, index) in educationList" :key="education.school_name + index">
            <el-button type="text" icon="el-icon-edit" class="edit"
                       @click="toggleEdit('editEducation',education, educationForm)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete-solid" class="remove"
                       @click="remove(education)">删除</el-button>
            <img v-if="$store.state.onlyReadResume" class="icon"
                 :src="require('@/image/illustration/el-icon-school.png')"
                 style="width: 45px; height: 45px; float: left" />
            <SymbolIcon v-else icon-class="el-icon-school" />
            <div class="education-info">
              <h3>
                <span>{{ education.collegeName }}</span>
                <span>
                      {{ education.education }}&nbsp;·
                      <span v-if="education.fullTime === 1">非</span>全日制
                      <el-divider direction="vertical"></el-divider>
                      {{ education.major }}
                </span>
                <span>
                    {{ education.startTime.split("-").slice(0,2).join(".") }}
                    &nbsp;-&nbsp;
                    {{ education.endTime.split("-").slice(0,2).join(".") }}</span>
              </h3>

            </div>
          </div>
        </div>
      </div>
      <div class="edit-education editDialog" v-else>
        <div class="edit-title">编辑教育经历</div>
        <div class="edit-content">
          <el-form :model="educationForm" :rules="educationRules" ref="educationForm" label-width="100px">
            <el-form-item label="学校名称" prop="collegeName">
              <el-input v-model="educationForm.collegeName"></el-input>
            </el-form-item>
            <el-form-item label="在校时间" required class="date-form-item">
              <el-form-item prop="startTime" label-width="0" class="start-date-form-item">
                <el-date-picker
                      v-model="educationForm.startTime"
                      type="month"
                      placeholder="入学时间"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
              <el-form-item prop="endTime" label-width="0" class="end-date-form-item">
                <el-date-picker
                      v-model="educationForm.endTime"
                      type="month"
                      placeholder="毕业时间"
                      format="yyyy-MM-dd"
                      value-format="yyyy-MM-dd">
                </el-date-picker>
              </el-form-item>
            </el-form-item>
            <el-form-item label="学历" prop="recruitmentId">
              <el-select v-model="user.recruitmentEducation.id" placeholder="请选择学历">
                <el-option
                      v-for="item in relist"
                      :key="item.id"
                      :label="item.title"
                      :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="专业名称" prop="major">
              <el-input v-model="educationForm.major"></el-input>
            </el-form-item>
            <el-form-item label-width="0">

              <el-form-item label="是否为全日制" prop="fullTime" label-width="128px">
                <el-switch
                      v-model="fullTime"
                      @change="((newStatus) => switchStatus(newStatus, 'fullTime'))"
                      active-color="#00d7c6"
                      inactive-color="#c0ccda">
                </el-switch>
              </el-form-item>
            </el-form-item>

            <el-form-item class="edit-btn">
              <el-button type="primary" @click="submitForm('educationForm','editEducation')">保存</el-button>
              <el-button type="text" @click="resetForm('editEducation')">取消</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
</template>

<script>
    import SymbolIcon from "@/components/SymbolIcon";
    export default {
        name: "ResumeEducation",
        components: { SymbolIcon},
        props: {
            resume_id: {
                type: String,
                required: true
            }
        },
        data() {
            return {
                 educationList: [],
              user:"",
              relist:{},
                educationForm: {
                   id:"",
                  collegeName: "",
                  education: "",
                  startTime: "",
                  endTime: "",
                    major: "",
                  fullTime:""

                },
                educationRules: {
                    school_name: [
                        { required: true, message: "请输入学校名称", trigger: 'blur'},
                    ],
                    education: [
                        { required: true, message: "请输入学历", trigger: 'blur'},
                    ],
                    start_date: [
                        { required: true, message: "请输入入学时间", trigger: 'blur'},
                    ],
                    end_date: [
                        { required: true, message: "请输入毕业时间", trigger: 'blur'},
                    ],
                    major: [
                        { required: true, message: "请输入所学专业", trigger: 'blur'},
                    ],
                },
                //教育经历 编辑框
                editEducation: false,

                examination_flag: true,
                fulltime_flag: true,
            }
        },
        created() {
            this.initData();
          this.initrelist();
          this.user=JSON.parse(window.localStorage.getItem("login_id"));
          this.educationList=this.user.educationalExperienceList;

        },
        methods: {
            async initData() {
                const res = await this.$axios.request({
                    url: `/education/list/${this.resume_id}`,
                    method: "get",
                });
                console.log(res);
                if(res.msg === 'success'){
                    this.educationList = Object.assign([],[],res.data.educationList);
                }
                // 获取完数据告知父组件已更新完毕,用于预览简历的异步通知
                this.$emit("updatePart:resume", 1)
            },
            // 切换为编辑框
            editOpen(editDialog){
                this[editDialog] = !this[editDialog];
            },
            // 切换为编辑框时 如果是编辑按钮，传递表单所需属性值
            toggleEdit(editDialog,defaultForm,editForm){
                if (defaultForm) {
                    for (const [defaultKey, defaultValue] of Object.entries(defaultForm)) {
                        for (const editKey of Object.keys(editForm)) {
                            if (editKey === defaultKey){
                                editForm[editKey] = defaultValue;
                            }
                        }
                    }
                    this.examination_flag = editForm.examination_flag === 'Y';
                    this.fulltime_flag = editForm.fulltime_flag === 'Y';

                }
                this.editOpen(editDialog);


            },
          async initrelist() {
            const res = await this.$axios.request({
              url: `web/user/listAll/`,
              method: "get",
            });
            if (res.data.msg === '查询成功') {
              this.relist=res.data.relist;
            }
          },
          async edlist() {
            const res = await this.$axios.request({
              url: `web/user/edlist?id=`+this.user.id,
              method: "get",
            });

            if (res.data.msg === '查询成功') {
              this.educationList=res.data.edlist;
            }
          },

            switchStatus(status, key) {
                status ? this.educationForm[key] = 'Y' : this.educationForm[key] = 'N';
                // console.log(this.educationForm)
            },
            // 教育经历删除
            async remove(education) {
                const res = await this.$axios.request({
                    url: `/web/user/deleteed?id=`+education.id,
                    method: "get",
                });
                if(res.data.msg=='删除成功'){
                  this.edlist();
                }

            },
            // 提交表单
            submitForm(formName, editDialog) {
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        const res = await this.$axios.request({
                            url: `/web/user/edsaveOrUpdate?userinfoId=`+this.user.id,
                            method: "post",
                            data: this.educationForm
                        });
                        if(res.data.msg === '修改成功'){
                          this.edlist();
                        }
                        this.$message.success("保存成功");
                        this.resetForm(editDialog);
                    } else {
                        this.$message.error("保存失败");
                        return false;
                    }
                });
            },

            // 重置表单
            resetForm(editDialog) {
                for (const key of Object.keys(this.educationForm)) {
                    if(key !== "resume_id") { 
                        this.educationForm[key] = ""
                    }
                }
                this.examination_flag = true;
                this.fulltime_flag = true;
                this.editOpen(editDialog);
            }
        },
    }
</script>

<style lang="less" scoped>
    .el-link-active{
        transition: .3s;
        &:hover{
            color: @mainColor;
        }
        &:active{
            color: @activeColor;
        }
    }

    @mainColor: #5dd5c8;
    @activeColor: #00c2b3;
    @background: #fff;
    @editBackground: #f8f9fc;
    @fontColor: #414a60;
    @salaryColor: #fb670f;
    @promptColor: #8d92a1;
    @borderColor: #dcdfe6;

    h2{
        font-size: 20px;
        padding-left: 20px;
        position: relative;
        &::before{
            content: "";
            display: block;
            width: 2px;
            height: 16px;
            position: absolute;
            border-radius: 3px;
            background: @activeColor;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
        }
        span{
            font-size: 15px;
            font-weight: normal;
            cursor: pointer;
            color: @activeColor;
          .el-link-active();
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            i{
                margin-right: 5px;
            }
        }
    }
    .content{
        margin-top: 30px;
        .education-wrapper{
            padding: 10px;
            position: relative;
            &:hover{
                box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.08);
                h3 >span:last-of-type{
                    display: none;
                }
                .edit,.remove{
                    display: block;
                }
            }
            .edit,.remove{
                display: none;
                position: absolute;
                right: 10px;
                top: 10px;
                z-index: 1;
                &.edit{
                    right: 80px;
                }
            }
            .icon{
                font-size: 45px;
                float: left;
                & + div{
                    margin-left: 70px;
                }
            }
        }
    }
    .edit-title{
        font-size: 16px;
        font-weight: bold;
        color: @activeColor;
    }
    .edit-content{
        margin-top: 30px;
        .el-form-item{
            width: 50%;
            &:not(.edit-btn){
                display: inline-block;
            }
            &.editor-form-item{
                width: 100%;
            }
            &.date-form-item{
                width: 50%;
                .el-date-editor.el-input, .el-date-editor.el-input__inner{
                    width: 100%;
                }
                .start-date-form-item /deep/ .el-form-item__content{
                    margin-right: 10px !important;
                }
                .end-date-form-item /deep/ .el-form-item__content{
                    margin-left: 10px !important;
                }
            }
        }
    }
    .editDialog{
        padding: 30px 40px;
        background: @editBackground;
    }
    
    .resume-education{
        font-size: 16px;
        .education{
            .education-info{
                h3{
                    position: relative;
                    > span:nth-child(1){
                        display: block;
                        font-size: 18px;
                        font-weight: bold;
                    }
                    > span:nth-child(2){
                        display: block;
                        line-height: 32px;
                        color: @promptColor;
                        font-size: 16px;
                        font-weight: normal;
                    }
                    > span:nth-child(3){
                        font-size: 16px;
                        font-weight: normal;
                        position: absolute;
                        right: 0;
                        top: 0;
                    }
                }
                > div{
                    margin-top: 20px;
                    h4{
                        font-weight: bold;
                        line-height: 32px;
                    }
                    p{
                        font-size: 14px;
                        line-height: 32px;
                    }
                }
            }
        }
    }

</style>